package com.jianz.campus_markets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.jianz.campus_markets.domain.ResponseResult;
import com.jianz.campus_markets.domain.dto.ProductDto;
import com.jianz.campus_markets.domain.entity.Product;
import com.jianz.campus_markets.mapper.ProductMapper;
import com.jianz.campus_markets.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianz.campus_markets.utils.BeanCopyUtils;
import com.jianz.campus_markets.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianz
 * @since 2023-11-22
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    @Override
    public ResponseResult productSearch(String condition) {

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getName,condition);
        List<Product> productList = getBaseMapper().selectList(queryWrapper);

        return ResponseResult.okResult(productList);
    }

    @Override
    public ResponseResult addProduct(ProductDto productDto) {

        //获取当前用户id
        Long userId = SecurityUtils.getUserId();

        Product product = BeanCopyUtils.copyBean(productDto,Product.class);

        product.setStatus(0);
        product.setUserid(Integer.valueOf(String.valueOf(userId)));
        save(product);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getUnauditedProduct() {

        //获取当前用户id
        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getUserid, userId);
        queryWrapper.eq(Product::getStatus,0);
        List<Product> products = getBaseMapper().selectList(queryWrapper);

        return ResponseResult.okResult(products);
    }

    @Override
    public ResponseResult getFailedProduct() {
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getUserid, userId);
        queryWrapper.eq(Product::getStatus,2);
        List<Product> products = getBaseMapper().selectList(queryWrapper);

        return ResponseResult.okResult(products);
    }

    @Override
    public ResponseResult getPassProduct() {
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getUserid, userId);
        queryWrapper.eq(Product::getStatus,1);
        List<Product> products = getBaseMapper().selectList(queryWrapper);

        return ResponseResult.okResult(products);
    }
    @Override
    public ResponseResult getPassProductById( int id) {

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getUserid, id);
        queryWrapper.eq(Product::getStatus,1);
        List<Product> products = getBaseMapper().selectList(queryWrapper);

        return ResponseResult.okResult(products);
    }

    @Override
    public ResponseResult deleteProduct(int productId) {
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getId, productId);
        queryWrapper.eq(Product::getUserid,userId);
        getBaseMapper().delete(queryWrapper);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateProduct(Product product) {
        product.setStatus(0);
        updateById(product);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getAllProduct() {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getStatus, 1);
        List<Product> products = getBaseMapper().selectList(queryWrapper);
        return ResponseResult.okResult(products);
    }

    @Override
    public ResponseResult getProductInfo(int productId) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getId,productId);
        Product product = getOne(queryWrapper);
        return ResponseResult.okResult(product);
    }
}
